package com.jiaai.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 需要转换类的superclazz
 * Created by Administrator on 2017/10/14.
 */
public abstract class MapSuperClazz implements Serializable {

    private static final long serialVersionUID = 1L;

    public Map<String,Object> toMap() throws IntrospectionException, InvocationTargetException, IllegalAccessException {

        Map<String, Object> map = new HashMap<String, Object>();

        BeanInfo beanInfo = Introspector.getBeanInfo(this.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (key.compareToIgnoreCase("class") == 0) {
                continue;
            }
            Method getter = property.getReadMethod();
            Object value = getter!=null ? getter.invoke(this) : null;
            map.put(key, value);
        }

        return map;
    }

}
